package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserBlogPeer extends BasePeer{
	public static final String tableName = "_User_Blog";
	public static final String INDEX = "index";
	public static final String BLOGID = "blogId";
	public static final String USERID = "userId";
	public static final String PHOTO = "photo";
	public static final String[] columnNames=new String[]{"index","blogId","userId","photo"};
	public static UserBlog getUserBlogByPKs(Object index, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogPeer.tableName,UserBlogPeer.columnNames," TRUE  AND `index`=? ",new Object[]{index,},0, 1,conn);
			UserBlog userBlog = new UserBlog();
		fill(rs,userBlog);
		return userBlog;
	}
	public static UserBlog getUserBlogReferenceColumnsOnlyByPKs(Object index, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogPeer.tableName,new String[]{"index",}," TRUE AND `index`=?",new Object[]{index,}, 0,  1,conn);
			UserBlog userBlog = new UserBlog();
		fill(rs,userBlog);
		return userBlog;
	}
	public static UserBlog getUserBlogByIndex(long index, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogPeer.tableName,UserBlogPeer.columnNames,"`index`=?",new Object[]{index},0, 1,conn);
			UserBlog userBlog = new UserBlog();
		fill(rs,userBlog);
		return userBlog;
	}
	public static UserBlog getUserBlogByBlogId(long blogId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogPeer.tableName,UserBlogPeer.columnNames,"`blogId`=?",new Object[]{blogId},0, 1,conn);
			UserBlog userBlog = new UserBlog();
		fill(rs,userBlog);
		return userBlog;
	}
	public static UserBlog getUserBlogByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogPeer.tableName,UserBlogPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UserBlog userBlog = new UserBlog();
		fill(rs,userBlog);
		return userBlog;
	}
	public static void fill(ResultSet rs, UserBlog userBlog) throws SQLException {
		if(rs.next()){
			try{userBlog.setIndex(rs.getLong("index"));}catch(Exception ex){}
			try{userBlog.setBlogId(rs.getLong("blogId"));}catch(Exception ex){}
			try{userBlog.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userBlog.setPhoto(rs.getString("photo"));}catch(Exception ex){}
			userBlog.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UserBlog> userBlogCollection) throws SQLException {
		while(rs.next()){
			UserBlog userBlog=new UserBlog();
			try{userBlog.setIndex(rs.getLong("index"));}catch(Exception ex){}
			try{userBlog.setBlogId(rs.getLong("blogId"));}catch(Exception ex){}
			try{userBlog.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userBlog.setPhoto(rs.getString("photo"));}catch(Exception ex){}
			userBlog.resetModified();
			userBlogCollection.add(userBlog);
		}
	}
	public static boolean update(UserBlog userBlog, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserBlogPeer.columnNames.length; i++) {
			if (((UserBlogBase)userBlog).modified[i]) {
				columns.add(UserBlogPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_User_Blog` SET ");
		sql.append("WHERE TRUE AND `index` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,userBlog.get(column));
		}
		pstmt.setObject(columns.size()+1+0,userBlog.get("index"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UserBlog userBlog, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (userBlog.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_User_Blog` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (userBlog.get(column) != null) {
				pstmt.setObject(i + 1 + j, userBlog.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UserBlog> getUserBlogCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UserBlog> collection=new Vector<UserBlog>();
		fill(rs,collection);
		return collection;
	}
}
